package com.xh.rufongwallpaper.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xh.rufongwallpaper.entity.Category;
import com.xh.rufongwallpaper.mapper.CategoryMapper;
import com.xh.rufongwallpaper.service.CategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * 分类服务实现类
 *
 * @author XH
 */
@Service
@RequiredArgsConstructor
public class CategoryServiceImpl implements CategoryService {

    private final CategoryMapper categoryMapper;

    @Override
    public Optional<Category> getCategoryById(Long id) {
        return Optional.ofNullable(categoryMapper.selectOne(
                new LambdaQueryWrapper<Category>()
                        .eq(Category::getId, id)
                        .eq(Category::getIsDeleted, false)
        ));
    }

    @Override
    public Optional<Category> getCategoryByCode(String code) {
        return Optional.ofNullable(categoryMapper.selectOne(
                new LambdaQueryWrapper<Category>()
                        .eq(Category::getId, code)
                        .eq(Category::getIsDeleted, false)
        ));
    }

    @Override
    public List<Category> getAllCategories() {
        return categoryMapper.selectList(
                new LambdaQueryWrapper<Category>()
                        .eq(Category::getIsDeleted, false)
                        .orderByAsc(Category::getSortOrder)
        );
    }

    /**
     * 分类是否存在
     *
     * @param categoryId 分类id
     * @return true 存在
     */
    @Override
    public boolean existsById(Long categoryId) {
        return categoryMapper.exists(Wrappers.lambdaQuery(Category.class)
                .eq(Category::getId, categoryId));
    }
} 